import { PartialType } from '@nestjs/mapped-types';
import { Transform, Type } from 'class-transformer';
import { IsArray, IsBoolean, IsEnum, IsNumber, IsOptional, IsString } from 'class-validator';
import { SortOrderEnum, StatusEnum } from '@/common/enums';

export class CreateRoleDto {
  @IsString()
  name: string;

  @IsString()
  @IsOptional()
  label?: string;

  @IsString({ each: true })
  @IsOptional()
  permissions?: string[];

  @IsNumber()
  @IsOptional()
  sort?: number;

  @IsString()
  @IsOptional()
  description?: string;
  
  @IsString()
  @IsOptional()
  remark?: string;

  @IsEnum(StatusEnum)
  @IsOptional()
  status?: StatusEnum;
}

export class FindRoleDto extends PartialType(CreateRoleDto) {
  @IsNumber()
  @IsOptional()
  id?: number;

  @IsString()
  @IsOptional()
  sortField?: string = 'sort';

  @IsEnum(SortOrderEnum) // 确保 sortOrder 只能是 'ASC'升序 或 'DESC'降序
  @Transform(({ value }) => value.toUpperCase()) // 将输入转换为大写
  @IsOptional()
  sortOrder?: SortOrderEnum = SortOrderEnum.ASC; // 默认值按 sort 字段升序

  @IsNumber()
  @IsOptional()
  pageNum?: number = 1;

  @IsNumber()
  @IsOptional()
  pageSize?: number = 20;
}

export class FindPermissionByRoleDto extends PartialType(CreateRoleDto) {
  @IsString({ each: true })
  roles: string[];
}

export class UpdateRoleDto extends PartialType(CreateRoleDto) {
  @IsNumber()
  id: number;
}

export class RemoveRoleDto extends PartialType(CreateRoleDto) {
  @IsNumber()
  id: number;
}

export class CreateBatchRoleDto {
  @IsArray()
  @Type(() => CreateRoleDto)
  list: CreateRoleDto[];

  @IsBoolean()
  @IsOptional()
  cover?: boolean = false;
}

export class RemoveBatchRoleDto {
  @IsArray()
  ids: number[];
}
